package com.zat.mes.andrmes.dofun.work.adapter

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.DayWorkRecordBean
import com.zat.mes.andrmes.utils.DateUtil
import com.zat.mes.andrmes.utils.SpannableStrUtil
import kotlinx.android.synthetic.main.item_item_proc_day_produce_plan.view.tvProcName
import kotlinx.android.synthetic.main.item_item_proc_day_record.view.*
import kotlinx.android.synthetic.main.item_item_proc_day_record.view.tvProcType

/**
 * @author zy
 * @date :2020-02-25 16:16
 * @description:做工记录 adapter
 */
class DayRecordPlanAdapter(layoutResId: Int) :
    BaseQuickAdapter<DayWorkRecordBean, BaseViewHolder>(layoutResId) {


    override fun convert(helper: BaseViewHolder, item: DayWorkRecordBean) {
        if (TextUtils.isEmpty(item?.machineno)){
            item?.machineno = ""
        }
        if (TextUtils.isEmpty(item?.machinename)){
            item?.machinename = ""
        }
        helper.setText(R.id.tvNameDayPlayItem, item?.productName)
            .setText(R.id.tvDateDayPlayItem, item?.workDay)
        var styleNo = context.getString(R.string.text_style) + " " + item?.styleNO
        var color = context.getString(R.string.text_color) + " " + item?.color
        var machineNo = context.getString(R.string.text_mac_no) + " " + item?.machineno
        var quantity = context.getString(R.string.text_amount) + " " + item?.quantity
        var size = context.getString(R.string.text_size) + " " + item?.size
        var machineName = context.getString(R.string.text_mac_name) + " " + item?.machinename
        val textColor = ContextCompat.getColor(context, R.color.color_text_grey)
        helper.setText(
            R.id.tvStyleNoDayPlayItem,
            SpannableStrUtil.getColorSpannable(styleNo, 3, styleNo.length, textColor)
        )
        helper.setText(
            R.id.tvColorDayPlayItem,
            SpannableStrUtil.getColorSpannable(color, 3, color.length, textColor)
        )
        helper.setText(
            R.id.tvMachineNoPlayItem,
            SpannableStrUtil.getColorSpannable(machineNo, 5, machineNo.length, textColor)
        )
        helper.setText(
            R.id.tvMachineNameDayPlayItem,
            SpannableStrUtil.getColorSpannable(machineName, 4, machineName.length, textColor)
        )
        helper.setText(
            R.id.tvQuantityDayPlayItem,
            SpannableStrUtil.getColorSpannable(quantity, 3, quantity.length, textColor)
        )
        helper.setText(
            R.id.tvSizeDayPlayItem,
            SpannableStrUtil.getColorSpannable(size, 3, size.length, textColor)
        )

        helper.setVisible(R.id.llBtnDayPlayShowHide, false)
        val llPro = helper.getView<LinearLayout>(R.id.llProcDayPlayItem)
        llPro.visibility = View.VISIBLE
        //for add view
        if (item?.list !== null) {
            llPro.removeAllViews()
            for (orderCraftprocess in item?.list) {
                val inflate = LayoutInflater.from(context)
                    .inflate(R.layout.item_item_proc_day_record, null)
                inflate.tvProcType.visibility = View.GONE
                inflate.tvProcName.text = orderCraftprocess.procName
                //开始时间 结束时间
                try {
                    inflate.tvStartTime.text = DateUtil.formatDate(
                        DateUtil.parseDate(
                            orderCraftprocess.beginTime,
                            DateUtil.FORMAT
                        ), DateUtil.FORMAT_s
                    )
                    inflate.tvEndTime.text = DateUtil.formatDate(
                        DateUtil.parseDate(
                            orderCraftprocess.finishTime,
                            DateUtil.FORMAT
                        ), DateUtil.FORMAT_s
                    )

                } catch (e: Exception) {

                }
                llPro.addView(inflate)
            }
        }


    }
}

